package com.google.mr.okhttptest.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.google.mr.okhttptest.R;
import com.google.mr.okhttptest.itemfragment.AllPlaylistFragment;
import com.google.mr.okhttptest.itemfragment.RankingFragment;
import com.google.mr.okhttptest.itemfragment.recommendFragment;
import com.google.mr.okhttptest.utils.ThemeUtils;
import android.support.design.widget.TabLayout;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

/**
 * Created by Administrator on 2017/11/27.
 */

public class TabNetPagerFragment extends BaseFragment {
    @BindView(R.id.tabs)
    TabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;

    @Override
    public int getContentViewLayoutID() {
        return R.layout.fragment_tabnetpager;
    }

    @Override
    protected void initView() {
        if (viewpager!=null){
            setUpViewPager(viewpager);
            viewpager.setOffscreenPageLimit(2);
        }
        tabLayout.setTabTextColors(R.color.text_color, ThemeUtils.getThemeColorStateList(mContext,R.color.theme_color_primary).getDefaultColor());
        tabLayout.setSelectedTabIndicatorColor(ThemeUtils.getThemeColorStateList(mContext,R.color.theme_color_primary).getDefaultColor());
        tabLayout.setupWithViewPager(viewpager);
    }

    @Override
    protected void initData() {

    }

    /**
     * Fragment与ViewPager
     * @param viewpager
     */
    private void setUpViewPager(ViewPager viewpager) {
        //创建Fragment适配器
        TabNetItemPagerFragmentAdapter tabNetItemPagerFragment = new TabNetItemPagerFragmentAdapter(getChildFragmentManager());
        tabNetItemPagerFragment.addFragment(new recommendFragment(),"新曲");
        tabNetItemPagerFragment.addFragment(new AllPlaylistFragment(),"歌单");
        tabNetItemPagerFragment.addFragment(new RankingFragment(),"排行榜");
        viewpager.setAdapter(tabNetItemPagerFragment);
    }

    private class TabNetItemPagerFragmentAdapter extends FragmentStatePagerAdapter {
        private List<Fragment> mFragments=new ArrayList<Fragment>();
        private List<String> mFragmentsTitle=new ArrayList<String>();

        public TabNetItemPagerFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String string) {
            mFragments.add(fragment);
            mFragmentsTitle.add(string);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentsTitle.get(position);
        }
    }
}
